//
//  HNSH_OrderDetailHeaderView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/1.
//

#import "HNSH_OrderDetailHeaderView.h"

//view
#import "HNSH_OrderDetailAddressView.h"

#import "ST_TimeDownToors.h"

@interface HNSH_OrderDetailHeaderView ()

/* topImgV */
@property (nonatomic, strong) UIImageView *topImgV;
/* statusIcon */
@property (nonatomic, strong) UIImageView *statusIcon;
/* statusLabel */
@property (nonatomic, strong) UILabel *statusLabel;

/* timeLabel */
@property (nonatomic, strong) UILabel *timeLabel;


/* 物流信息 */
@property (nonatomic, strong) UIView *wlBgView;
/* wlTitleLabel */
@property (nonatomic, strong) UILabel *wlTitleLabel;
/* wlTimeLabel */
@property (nonatomic, strong) UILabel *wlTimeLabel;
/* wlIcon */
@property (nonatomic, strong) UIImageView *wlIcon;
/* wlShowIcon */
@property (nonatomic, strong) UIImageView *wlShowIcon;

/* addressView */
@property (nonatomic, strong) HNSH_OrderDetailAddressView *addressView;

@end

@implementation HNSH_OrderDetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor appBGColor];
        [self configUI];
    }
    return self;
}

- (void)setModel:(HNSH_OrderDetailModel *)model {
    _model = model;
    self.statusLabel.text = model.order_status_text;
    //有物流信息
    if ([model.shipping_code isNotBlank]) {
        self.topImgV.frame = CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight+150);
    } else {
        self.topImgV.frame = CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight+54);
    }
    //订单状态 10:待付款 20:发货中 30:待收货 100:已完成 110:已取消 120:付款失败 200:已退款
    if (model.order_status == 110 || model.order_status == 120 || model.order_status == 200) {
        self.statusIcon.image = [UIImage imageNamed:@"mine_order_detail_icon_close"];
    } else {
        self.statusIcon.image = [UIImage imageNamed:@"mine_order_detail_icon_daifu"];
        self.timeLabel.text = model.shipping_name;
        self.wlTimeLabel.text = model.shipping_time;
        if (model.order_status == 10) {
            WeakSelf(weakSelf);
            [[ST_TimeDownToors shareTimer] startTimerWithTime:[model.payment_timeout_seconds integerValue] complate:^(NSInteger time) {
                NSString *timeStr = [NSString stringWithFormat:@"%ld:%ld:%ld",time/3600,(time%3600)/60,time%60];
                weakSelf.timeLabel.text = timeStr;
                if (time == 0) {
                    
                }
            }];
        } else if (model.order_status == 20 || model.order_status == 30) {
            self.wlTitleLabel.text = @"您的订单已进入库房，准备出库";
        } else if (model.order_status == 100) {
            self.statusIcon.image = [UIImage imageNamed:@"mine_order_detail_icon_wan"];
            self.wlTitleLabel.text = @"订单已签收";
        }
    }
    
    self.addressView.model = model;
}

- (void)configUI {
    [self configTopView];
    
    [self configBottomView];
}

- (void)configTopView {
    [self addSubview:self.topImgV];
    [self.topImgV addSubview:self.statusIcon];
    [self.topImgV addSubview:self.statusLabel];
    
    [self.topImgV addSubview:self.timeLabel];
    
    [self.topImgV addSubview:self.wlBgView];
    [self.wlBgView addSubview:self.wlIcon];
    [self.wlBgView addSubview:self.wlTitleLabel];
    [self.wlBgView addSubview:self.wlTimeLabel];
    [self.wlBgView addSubview:self.wlShowIcon];
}

- (void)configBottomView {
    [self addSubview:self.addressView];
    
    [self.addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(88);
    }];
}

#pragma mark lazy
- (UIImageView *)topImgV {
    if (!_topImgV) {
        _topImgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight+54)];
        _topImgV.image = [UIImage imageNamed:@"mine_order_detail_top_bg"];
        _topImgV.clipsToBounds = YES;
    }
    return _topImgV;
}

- (UIImageView *)statusIcon {
    if (!_statusIcon) {
        _statusIcon = [[UIImageView alloc]initWithFrame:CGRectMake(16, kNavBarAndStatusBarHeight+16, 20, 20)];
        _statusIcon.image = [UIImage imageNamed:@"mine_order_detail_icon_daifu"];
    }
    return _statusIcon;
}

- (UILabel *)statusLabel {
    if (!_statusLabel) {
        _statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(44, kNavBarAndStatusBarHeight+16, 200, 20)];
        _statusLabel.text = @"";
        _statusLabel.textColor = [UIColor appWhiteLabelColor];
        _statusLabel.font = [UIFont boldSystemFontOfSize:16];
    }
    return _statusLabel;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth-216, kNavBarAndStatusBarHeight+16, 200, 20)];
        _timeLabel.text = @"";
        _timeLabel.textColor = [UIColor appWhiteLabelColor];
        _timeLabel.font = [UIFont boldSystemFontOfSize:12];
        _timeLabel.hidden = YES;
    }
    return _timeLabel;
}

- (UIView *)wlBgView {
    if (!_wlBgView) {
        _wlBgView = [[UIView alloc]initWithFrame:CGRectMake(16, kNavBarAndStatusBarHeight+54, kScreenWidth-32, 80)];
        _wlBgView.backgroundColor = [UIColor appWhiteLabelColor];
        _wlBgView.layer.cornerRadius = 10.0f;
        _wlBgView.layer.masksToBounds = YES;
    }
    return _wlBgView;
}

- (UILabel *)wlTitleLabel {
    if (!_wlTitleLabel) {
        _wlTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(62, 21, 200, 20)];
        _wlTitleLabel.text = @"";
        _wlTitleLabel.textColor = [UIColor appBlackLabelColor];
        _wlTitleLabel.font = [UIFont boldSystemFontOfSize:14];
    }
    return _wlTitleLabel;
}

- (UILabel *)wlTimeLabel {
    if (!_wlTimeLabel) {
        _wlTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(62, 43, 200, 17)];
        _wlTimeLabel.text = @"";
        _wlTimeLabel.textColor = [UIColor appGrayLabelColor];
        _wlTimeLabel.font = [UIFont systemFontOfSize:12];
    }
    return _wlTimeLabel;
}

- (UIImageView *)wlIcon {
    if (!_wlIcon) {
        _wlIcon = [[UIImageView alloc]initWithFrame:CGRectMake(14, 20, 40, 40)];
        _wlIcon.image = [UIImage imageNamed:@"mine_order_detail_icon_wuliu"];
    }
    return _wlIcon;
}

- (UIImageView *)wlShowIcon {
    if (!_wlShowIcon) {
        _wlShowIcon = [[UIImageView alloc]initWithFrame:CGRectMake(self.wlBgView.width-34, 30, 20, 20)];
        _wlShowIcon.image = [UIImage imageNamed:@"mine_order_detail_jinru"];
    }
    return _wlShowIcon;
}

- (HNSH_OrderDetailAddressView *)addressView {
    if (!_addressView) {
        _addressView = [[HNSH_OrderDetailAddressView alloc]init];
    }
    return _addressView;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
